package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.example.myapp.entity.UserInfo;

import cn.leancloud.LCObject;
import cn.leancloud.LCQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class UpdatePwdActivity extends AppCompatActivity {
    private EditText et_new_pwd;
    private EditText et_confirm_pwd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_pwd);

        //初始化控件
        et_new_pwd = findViewById(R.id.et_new_pwd);
        et_confirm_pwd = findViewById(R.id.et_confirm_pwd);

        //修改密码
        findViewById(R.id.btn_update_pwd).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String new_pwd = et_new_pwd.getText().toString();
                String confirm_pwd = et_confirm_pwd.getText().toString();

                if(TextUtils.isEmpty(new_pwd) || TextUtils.isEmpty(confirm_pwd)){
                    Toast.makeText(UpdatePwdActivity.this, "新密码不能为空", Toast.LENGTH_SHORT).show();
                }else if(!new_pwd.equals(confirm_pwd)){
                    Toast.makeText(UpdatePwdActivity.this, "新密码与确认密码不一致", Toast.LENGTH_SHORT).show();
                }else {

                    UserInfo userInfo = UserInfo.getUserInfo();
                    String username = userInfo.getUsername();
                    if(null!=userInfo){
                        LCQuery<LCObject> query = new LCQuery<>("UserTable");
                        query.whereEqualTo("username", username);
                        query.getFirstInBackground().subscribe(new Observer<LCObject>() {
                            public void onSubscribe(Disposable disposable) {}
                            public void onNext(LCObject todo) {
                                todo.put("password",new_pwd);
                                todo.saveInBackground().subscribe(new Observer<LCObject>() {
                                    public void onSubscribe(Disposable disposable) {}
                                    public void onNext(LCObject todo) {
                                        // 成功保存之后，执行其他逻辑
                                        Toast.makeText(UpdatePwdActivity.this, "密码修改成功，请重新登录", Toast.LENGTH_SHORT).show();
                                        //回传的时候要用 startActivityForResult启动一个页面，并且在该页面要setResult
                                        setResult(1000);
                                        Intent intent = new Intent(UpdatePwdActivity.this, LoginActivity.class);
                                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                        finish();
                                    }
                                    public void onError(Throwable throwable) {
                                        // 异常处理
                                    }
                                    public void onComplete() {}
                                });
                            }
                            public void onError(Throwable throwable) {}
                            public void onComplete() {}
                        });


                    }

                }
            }
        });

        //返回
        findViewById(R.id.toolbar).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


    }
}